package com.hanyxx.thread;

import lombok.SneakyThrows;

/**
 * 模拟线程不同步导致脏读问题
 * @author layman
 * @date 2021/2/5
 */
public class Demo07 {
    public static void main(String[] args) throws InterruptedException {
        UserService service = new UserService();
        Demo07Thread thread = new Demo07Thread(service);
        thread.start();
        Thread.sleep(500);
        service.getValue();
    }
}
class UserService{
    private String username = "layman";
    private String password = "12345";

    @SneakyThrows
    public void change(String username, String password){
        this.username = username;
        Thread.sleep(1000);
        this.password = password;
        System.out.println("线程名称：["+Thread.currentThread().getName()+"] ,change方法执行,username: "+username+" ,password: "+password);
    }
    public void getValue(){
        System.out.println("线程名称：["+Thread.currentThread().getName()+"] ,getValue方法执行,username: "+username+" ,password: "+password);
    }
}
class Demo07Thread extends Thread{
    private UserService service;
    public Demo07Thread(UserService service){
        this.service = service;
    }
    @Override
    public void run() {
        service.change("菠菜饭团","54321");
    }
}
